package cn.ryh.qiyuesuo;

import com.qiyuesuo.sdk.v2.SdkClient;
import com.qiyuesuo.sdk.v2.bean.*;
import com.qiyuesuo.sdk.v2.json.JSONUtils;
import com.qiyuesuo.sdk.v2.request.ContractDraftRequest;
import com.qiyuesuo.sdk.v2.request.DocumentDownloadRequest;
import com.qiyuesuo.sdk.v2.response.SdkResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 * @author Raoyh
 * @Description
 * @create 2024-08-05 20:04
 */
@Slf4j
public class Test {
    public static void main(String[] args) throws IOException {
        String base64 = getBase64();
        SdkClient sdkClient = new SdkClient("https://openapi.qiyuesuo.cn", "TjtZ82OV9H", "i9azRfqrkj4nPFtTeAZRBLUs0gF9iJ");
        ContractDraftRequest contractDraftRequest = new ContractDraftRequest();
        Contract contract = new Contract();
        contract.setSubject("职评系统盖章");
        contract.setOrdinal(true);
        contract.setSend(true);
        Category category = new Category();
        category.setId(3253974316785669056L);
        category.setName("高级-A4");
        contract.setCategory(category);
        List<Signatory> signatories = new ArrayList<>();
        Signatory signatory = new Signatory();
        signatory.setTenantType("COMPANY");
        signatory.setTenantName("中国建设科技有限公司");
        List<Action> actions = new ArrayList<>();
        Action action = new Action();
        action.setType("COMPANY");
        action.setName("盖高级章");
        actions.add(action);
        signatory.setActions(actions);
        signatory.setDelaySet(false);
        signatories.add(signatory);
        contract.setSignatories(signatories);
        List<TemplateParam> templateParams = new ArrayList<>();
        TemplateParam templateParam = new TemplateParam();
        templateParam.setName("xingming");
        templateParam.setValue("刘丙恒");
        templateParams.add(templateParam);
        templateParam = new TemplateParam();
        templateParam.setName("xingbie");
        templateParam.setValue("男");
        templateParams.add(templateParam);
        templateParam = new TemplateParam();
        templateParam.setName("shenfenzheng");
        templateParam.setValue("321456987412583691");
        templateParams.add(templateParam);
        templateParam = new TemplateParam();
        templateParam.setName("参数8");
        templateParam.setValue(base64);
        templateParams.add(templateParam);
        templateParam = new TemplateParam();
        templateParam.setName("zhuanye");
        templateParam.setValue("zhuanye");
        templateParams.add(templateParam);
        templateParam = new TemplateParam();
        templateParam.setName("zige");
        templateParam.setValue("zige");
        templateParams.add(templateParam);
        templateParam = new TemplateParam();
        templateParam.setName("shijian");
        templateParam.setValue("shijian");
        templateParams.add(templateParam);
        templateParam = new TemplateParam();
        templateParam.setName("bianhao");
        templateParam.setValue("bianhao");
        templateParams.add(templateParam);
        contract.setTemplateParams(templateParams);
        contractDraftRequest.setContract(contract);
        String response = sdkClient.service(contractDraftRequest);
        SdkResponse<Contract> responseObj = JSONUtils.toQysResponse(response, Contract.class);
        Long id = null;
        if("00000000".equals(responseObj.getResponseCode())) {
            Contract result = responseObj.getResult();
            id = result.getDocuments().get(0).getId();
            log.info("REQUEST SUCCESS{}", JSONUtils.toJson(result));
        } else {
            System.out.println("REQUEST FAILED，错误码:" + responseObj.getResponseCode() +"，错误信息:" + responseObj.getMessage());
        }
        extracted(id);
    }

    private static void extracted(Long id) throws IOException {
        System.out.println(id);
        SdkClient sdkClient = new SdkClient("https://openapi.qiyuesuo.cn", "TjtZ82OV9H", "i9azRfqrkj4nPFtTeAZRBLUs0gF9iJ");
        DocumentDownloadRequest documentDownloadRequest = new DocumentDownloadRequest();
        documentDownloadRequest.setDocumentId(id);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        sdkClient.download(documentDownloadRequest, outputStream);
        Path filenull = Files.write(Paths.get("d://test.pdf"), outputStream.toByteArray(), StandardOpenOption.CREATE);
    }

    private static String getBase64()throws IOException {
        try {
            // 图片文件的路径
            String imagePath = "C:\\Users\\Ryan\\Documents\\1222.png";
            // 读取图片文件到字节数组
            byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
            // 对字节数组进行Base64编码
            String encodedImage = Base64.getEncoder().encodeToString(imageBytes);
            // 输出Base64编码的字符串
            System.out.println("Base64 encoded string: " + encodedImage);
            // 如果需要，你可以将MIME类型添加到Base64编码字符串前面
            String base64ImageWithMimeType = "data:image/png;base64," + encodedImage;
            System.out.println("Base64 encoded string with MIME type: " + base64ImageWithMimeType);
            return base64ImageWithMimeType;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
